<?php

	$loginEmailAddressErrorMessage = '';
	$loginPasswordErrorMessage = '';
	$emailAddress = '';
	
	if(isset($_POST['btnLogin']))
	{
		$emailAddress = $_POST['email_address'];
		$loginValidationError = new validation();
		
		$loginEmailAddressErrorMessage = $loginValidationError->isEmpty(trim($_POST['email_address']),'<label class="required">Your email address is required. </label>');
		$LoginPasswordErrorMessage = $loginValidationError->isEmpty(trim($_POST['password']),'<label class="required">Your password is required. </label>');
		
		$loginMessage = '';
	}
?>

<?php
	if(isset($_POST['btnLogin']))
	{	
		if($loginValidationError->status == 0)
		{
			try
			{
				if($currentUser->loginUser(trim($_POST['email_address']), md5($_POST['password'])))
				{
					//echo getPreviousPage(); exit();
					$loginMessage = "logged in";
					
					$cartItem = new UserCartItem(); 
					
					$cartItem->userId = $currentUser->id;
					$cartItemCount = $cartItem->countCartItems();
					
					if($cartItemCount !== false)
					{
						$_SESSION['cartCount'] = $cartItemCount;
					}
					
					header("Location: ".getPreviousPage());
					//echo getPreviousPage();
					exit();
				}
				else
				{
					$loginMessage = 'Email Address or Password is incorrect.';
				}
			}
			catch(Exception $ex)
			{
				$loginMessage = $ex->getMessage();
			}
		}
	}
?>